package com.erpanderp.wam.usm.view.leave;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.jboss.resteasy.spi.HttpRequest;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.selectonebutton.SelectOneButton;
import org.primefaces.context.RequestContext;

import com.erpanderp.wam.adm.model.service.RolePermissionDAO;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.leave.EmployeeLeaveDAO;
import com.erpanderp.wam.usm.model.service.leave.LeaveBalanceDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMHostManager;
import com.erpanderp.wam.util.WAMUtil;

@ManagedBean
@ViewScoped
public class ViewLeavesBean {
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session
			.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	HttpServletRequest request = (HttpServletRequest) FacesContext
			.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private Map<String, Integer> monthsMap = CommonLookup.getMonths();
	private Map<String, Integer> yearsMap = WAMUtil.getLastSixYears();
	private Integer selectedMonth;
	private Integer selectedYear;
	private String selectedButton = CommonLookup.LEAVES_TO_BE_APPROVED;
	private String employeeName;
	String monthNames[] = new DateFormatSymbols().getMonths();
	private boolean LEAVES_FOUND;
	private boolean LEAVES_TO_BE_CANCELLED_FOUND;
	private boolean LEAVES_NOT_FOUND;
	private boolean LEAVES_TO_BE_CANCELLED_NOT_FOUND;
	private List<EmployeeLeave> employeeLeavesToApproved = new ArrayList<EmployeeLeave>();
	private List<EmployeeLeave> employeeLeavesToCancelled = new ArrayList<EmployeeLeave>();
	private EmployeeLeave selectedEmployeeLeave = new EmployeeLeave();
	RolePermissionDAO rolePermissionDAO = new RolePermissionDAO();
	private EmployeeLeaveDAO employeeLeaveDAO = new EmployeeLeaveDAO();
	private String leaveCODE = null;
	private EmployeeLeave employeeLeave = new EmployeeLeave();

	private boolean FORM_VALID;

	private EmailUtil emailUtil;
	private String leaveApprovedAlready;
	private boolean leaveApprovedFlag = true;
	private LeaveBalance leaveBalance = new LeaveBalance();
	private Float actualLeaveBalance;
	private Float currentLeaveBalance;
	private Float diductedLeaves;
	private Float remainingLeaves;
	private LeaveBalanceDAO leaveBalanceDAO = new LeaveBalanceDAO();
	private HtmlOutputText leaveApprovedAlreadyHtmlOutputText = new HtmlOutputText();
	private SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
	private EmployeeAndReporting employeeAndReporting;
	private EmployeeAndReportingDAO employeeAndReportingDAO = new EmployeeAndReportingDAO();
	private String employeeCode;
	private Employee employee;
	private EmployeeDAO employeeDAO = new EmployeeDAO();
	private Employee approver;
	private Float addingLeaves;
	private Float toBeAddedLeaves;
	private TimeZone defaultTimeZone = TimeZone.getDefault();
	private String leaveFROM;
	private String leaveTO;
	private String leaveFromToBeDisplayed;
	private String leaveToToBeDisplayed;
	private List<EmployeeLeave> employeeLeavesToBeApproved;

	public ViewLeavesBean() {
		if (wamUser != null) {
			employeeAndReporting = employeeAndReportingDAO.getEmployeeReportingDetail(wamUser.getEmployee().getEmployeeId());
			approver = employeeAndReporting.getProjectManager();

			if (request.getParameter("redirectfrom") != null && request.getParameter("redirectfrom").equals(CommonLookup.PAGE_DASHBOARD)) {
				selectedButton = CommonLookup.LEAVES_TO_BE_APPROVED;
				performAction(selectedButton);
			}

		} else {
			ValidationUtil.gotoLoginPage();
		}
		performAction(selectedButton);
	}

	public void searcActionListener() {
		System.out.println("IN LISTENER");
	}

	public String approveRejectLeaveAction(ActionEvent actionEvent) {
		UIComponent uiData = actionEvent.getComponent();
		String messageString = null;
		employeeAndReporting = employeeAndReportingDAO.getEmployeeReportingDetail(wamUser.getEmployee().getEmployeeId());
		approver = employeeAndReporting.getProjectManager();
		leaveFROM = formatter.format(selectedEmployeeLeave.getPeriodFrom());
		leaveTO = formatter.format(selectedEmployeeLeave.getPeriodTo());
		System.out.println("ACTION COMMAND  IS :"
				+ actionEvent.getComponent().getId());
		System.out.println("IN APPROVE LEAVE ACTION");
		validateApproval();
		if (FORM_VALID) {


				
				Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
				emailUtil = new EmailUtil();
				if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
					System.out.println("PRODUCTION");
					String[] to = { selectedEmployeeLeave.getEmployee().getEmail() };
					emailUtil.setRecieverAddresses(to);
				} else {
					System.out.println("DEVELOPMENT");
					String[] to = { hostsMap.get(CommonLookup.DEV_EMAIL) };
					emailUtil.setRecieverAddresses(to);
				}
				
				if (actionEvent.getComponent().getId().equals("Approve")) {
					selectedEmployeeLeave.setApprovalStatus(CommonLookup.LEAVE_APPROVED_STATUS);
					employeeLeaveDAO.leaveApprovedByApprovalComments(selectedEmployeeLeave);
					
					
					//Update timesheet if exist
					System.out.println("Timesheet Updated: " + updateTimesheetIfExist());
					
					messageString = "Dear "
							+ selectedEmployeeLeave.getEmployee()
									.getFirstName()
							+ " "
							+ selectedEmployeeLeave.getEmployee().getLastName()
							+ ",<br /><br />"
							+

							"<table border='0'>"
							+ "<tr> <td>Leave Description </td><td> : </td><td>"
							+ selectedEmployeeLeave.getReason()
							+ " </td></tr>"
							+ "<tr> <td>Leave Type</td><td> : </td> <td>"
							+ selectedEmployeeLeave.getLeaveType()
									.getLeaveType() + "</td></tr>" +

							"</table><br/><br/>" +

							"Your leave request is approved from " + leaveFROM
							+ " to " + leaveTO + " with approver comments : "
							+ selectedEmployeeLeave.getApprovalComments()
							+ "<br /><br />" +

							"Thanks & Regards, <br />WAM - Dev TEAM";
				} else {
					System.out.println("COMPONENT ID IS :"+ actionEvent.getComponent().getId());
					selectedEmployeeLeave.setApprovalStatus(CommonLookup.LEAVE_REJECT_STATUS);
					employeeLeave.setApprovalComments(selectedEmployeeLeave.getApprovalComments());
					employeeLeaveDAO.leaveRejectedByApprovalComments(selectedEmployeeLeave);
					FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO," Leave Request Rejected.", ""));
					RequestContext.getCurrentInstance().execute("dialog.hide()");
					messageString = "Dear "
							+ selectedEmployeeLeave.getEmployee()
									.getFirstName()
							+ " "
							+ selectedEmployeeLeave.getEmployee().getLastName()
							+ ",<br /><br />"
							+

							"<table border='0'>"
							+ "<tr> <td>Leave Description </td><td> : </td><td>"
							+ selectedEmployeeLeave.getReason()
							+ " </td></tr>"
							+ "<tr> <td>Leave Type</td><td> : </td> <td>"
							+ selectedEmployeeLeave.getLeaveType()
									.getLeaveType() + "</td></tr>" +

							"</table><br/><br/>" +

							"Your leave request is rejected from " + leaveFROM
							+ " to " + leaveTO + "with approver comments : "
							+ selectedEmployeeLeave.getApprovalComments()
							+ "<br /><br />" +

							"Thanks & Regards, <br />WAM - Dev TEAM";
				}
				emailUtil.setMessage(messageString);
				
				emailUtil.setSenderEmail("podquick@erpanderp.com",
						"WAM - Dev Team");

				if (actionEvent.getComponent().getId().equals("Approve")) {
					selectedEmployeeLeave.setApprovalStatus(CommonLookup.LEAVE_APPROVED_STATUS);
					employeeLeaveDAO.leaveApprovedByApprovalComments(employeeLeave);
					leaveBalance = new LeaveBalance();
					leaveBalance = leaveBalanceDAO.getActualLeaveBalanceByLeaveTypeIdAndEmployeeId(
						selectedEmployeeLeave.getLeaveType().getLeaveTypeid(),selectedEmployeeLeave.getEmployee().getEmployeeId());

					if (selectedEmployeeLeave.getLeaveType().getLeaveType().equals(CommonLookup.WORKFROMHOME_TYPE)) {
						System.out.println("APPROVED LEAVE TYPE IN WH IF IS-----------------> ="
										+ selectedEmployeeLeave.getLeaveType().getLeaveType());
						remainingLeaves = leaveBalance.getBalance()+ selectedEmployeeLeave.getNoOfDays();
					} else if (selectedEmployeeLeave.getLeaveType().getLeaveType()
							.equals(CommonLookup.LOSS_OF_PAY_TYPE)) {
						System.out.println("APPROVED LEAVE TYPE IN LOP IF IS----------------> ="
										+ selectedEmployeeLeave.getLeaveType().getLeaveType());
						remainingLeaves = leaveBalance.getBalance()+ selectedEmployeeLeave.getNoOfDays();
					}else if (selectedEmployeeLeave.getLeaveType().getLeaveType().equals(CommonLookup.COMP_OF_TYPE)) {
						System.out.println("APPROVED LEAVE TYPE IN COMP OFF IF IS----------------> ="+ 
					selectedEmployeeLeave.getLeaveType().getLeaveType());
						remainingLeaves = leaveBalance.getBalance()
								+ selectedEmployeeLeave.getNoOfDays();
					}else {
						System.out.println("APPROVED LEAVE TYPE IN ELSE IS ="+ selectedEmployeeLeave.getLeaveType()
										.getLeaveType());

						remainingLeaves = leaveBalance.getBalance()- selectedEmployeeLeave.getNoOfDays();

					}
					leaveBalance.setBalance(remainingLeaves);
					leaveBalance.getLeaveType().setLeaveTypeid(selectedEmployeeLeave.getLeaveType().getLeaveTypeid());

					leaveBalanceDAO.updateLeaveBalance(leaveBalance);
					emailUtil.setSubject(selectedEmployeeLeave.getEmployee().getFirstName()+ " ("
							+ selectedEmployeeLeave.getLeaveCode()
							+ ") "
							+ "Leave Request is Approved ");
					FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(
											FacesMessage.SEVERITY_INFO," Leave Request Approved successfully.",""));
					RequestContext.getCurrentInstance().execute("dialog.hide()");
				} else {
					emailUtil.setSubject(selectedEmployeeLeave.getEmployee().getFirstName()
							+ " ("
							+ selectedEmployeeLeave.getLeaveCode()
							+ ") "
							+ "Leave Request is Rejected");
				}

				SwingWorker worker = new SwingWorker<String, Object>() {

					@Override
					protected String doInBackground() throws Exception {
						// TODO Auto-generated method stub

						emailUtil.processAndSendEmail();
						return null;
					}

					@Override
					protected void done() {
						// TODO Auto-generated method stub
						System.out.println("MAIL SENT");
						// super.done();
					}
				};
				worker.execute();

				selectedEmployeeLeave = new EmployeeLeave();
			
			return "viewleaves";
		} else {
			RequestContext.getCurrentInstance().execute("dialog.show()");
			return "viewleaves";
		}
		

	}


	
	private boolean updateTimesheetIfExist(){
		boolean UPDATED = false;
		System.out.println("trying to update timesheet...");
		try{
			TimesheetDetailDAO timesheetDetailDAO = new TimesheetDetailDAO();
			List<TimesheetDetail> timesheetDetails = (List<TimesheetDetail>) timesheetDetailDAO.getTimesheetDetails(selectedEmployeeLeave.getPeriodFrom(), selectedEmployeeLeave.getPeriodTo(), selectedEmployeeLeave.getEmployee().getEmployeeId());
			System.out.println(timesheetDetails != null ? timesheetDetails.size() : "ZEROOOOOOOOOOOO");
			Date now = new Date();
			for(TimesheetDetail timesheetDetail : timesheetDetails){
				timesheetDetail.setDateModified(now);
				timesheetDetail.setModifiedBy(wamUser.getEmployee().getFirstName());
				timesheetDetail.setTimesheetDetailStatus(selectedEmployeeLeave.getLeaveType().getLeaveType());
				
				timesheetDetailDAO.updateTimesheetDetail(timesheetDetail);
				
			}
			return UPDATED;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}				
	}
	
	
	private boolean updateTimesheetDetailStatusIfExist(){
		boolean UPDATED = false;
		System.out.println("trying to update timesheet...");
		try{
			TimesheetDetailDAO timesheetDetailDAO = new TimesheetDetailDAO();
			List<TimesheetDetail> timesheetDetails = (List<TimesheetDetail>) timesheetDetailDAO.getTimesheetDetails(selectedEmployeeLeave.getPeriodFrom(), selectedEmployeeLeave.getPeriodTo(), selectedEmployeeLeave.getEmployee().getEmployeeId());
			System.out.println(timesheetDetails != null ? timesheetDetails.size() : "ZEROOOOOOOOOOOO");
			Date now = new Date();
			for(TimesheetDetail timesheetDetail : timesheetDetails){
				timesheetDetail.setDateModified(now);
				timesheetDetail.setModifiedBy(wamUser.getEmployee().getFirstName());
				timesheetDetail.setTimesheetDetailStatus("P");
				
				timesheetDetailDAO.updateTimesheetDetail(timesheetDetail);
				
			}
			return UPDATED;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}				
	}
	

	public void buttonChangeListner(ActionEvent e) {
		selectedButton = (((MenuItem)e.getComponent()).getValue().toString());	

		performAction(selectedButton);
		System.out.println("SELECTED BUTTON =" + selectedButton);
	}
	
	public void performAction(String selectedButton){
		if (selectedButton.equals(CommonLookup.LEAVES_TO_BE_APPROVED)) {
            System.out.println(request.getParameter("redirectedfrom"));
			if(request.getParameter("redirectfrom") != null && request.getParameter("redirectfrom").equals(CommonLookup.PAGE_DASHBOARD)){
				employeeLeavesToApproved = employeeLeaveDAO.getLeaves(wamUser.getEmployee().getEmployeeId(), CommonLookup.LEAVE_INPROCESS_STATUS);
				
			}else{
				employeeLeavesToApproved = employeeLeaveDAO.getLeaves(wamUser.getEmployee().getEmployeeId(), CommonLookup.LEAVE_INPROCESS_STATUS);	
			}
			
			if (employeeLeavesToApproved != null
					&& employeeLeavesToApproved.size() > 0) {
				LEAVES_FOUND = true;
				LEAVES_TO_BE_CANCELLED_FOUND = false;
				LEAVES_TO_BE_CANCELLED_NOT_FOUND = false;
				LEAVES_NOT_FOUND = false;
			} else {
				LEAVES_TO_BE_CANCELLED_FOUND = false;
				LEAVES_TO_BE_CANCELLED_NOT_FOUND = false;
				LEAVES_NOT_FOUND = true;
			}

		} else {

			employeeLeavesToCancelled = employeeLeaveDAO.getCancellationInprocessLeavesListUnderWAMUserCompetency(CommonLookup.LEAVE_CANCELLATION_INPROCESS_STATUS, wamUser.getEmployee().getEmployeeId());
			if (employeeLeavesToCancelled != null && employeeLeavesToCancelled.size() > 0) {
				LEAVES_FOUND = false;
				LEAVES_TO_BE_CANCELLED_FOUND = true;
				LEAVES_TO_BE_CANCELLED_NOT_FOUND = false;
				LEAVES_NOT_FOUND = false;
			} else {
				LEAVES_FOUND = false;
				LEAVES_NOT_FOUND = false;
				LEAVES_TO_BE_CANCELLED_NOT_FOUND = true;
			}

		}
	}

	public String leveCancelRequestApprovedORRejectAction(ActionEvent actionEvent) {
		UIComponent uiData = actionEvent.getComponent();
		String messageString = null;

		String leaveFROM = formatter.format(selectedEmployeeLeave
				.getPeriodFrom());
		String leaveTO = formatter.format(selectedEmployeeLeave.getPeriodTo());
		System.out.println("ACTION COMMAND  IS :"
				+ actionEvent.getComponent().getId());

		validate();
		if (FORM_VALID) {
			leaveBalance = leaveBalanceDAO.getActualLeaveBalanceByLeaveTypeIdAndEmployeeId(
							selectedEmployeeLeave.getLeaveType().getLeaveTypeid(), selectedEmployeeLeave.getEmployee().getEmployeeId());
			if (actionEvent.getComponent().getId().equals("Accept")) {
				if (selectedEmployeeLeave != null && selectedEmployeeLeave.getLeaveType().getLeaveType()
								.equals(CommonLookup.WORKFROMHOME_TYPE)) {
					
					addingLeaves = Float.valueOf(selectedEmployeeLeave.getNoOfDays());
					toBeAddedLeaves = leaveBalance.getBalance()-addingLeaves;

				} else if (selectedEmployeeLeave != null && selectedEmployeeLeave.getLeaveType().getLeaveType()
								.equals(CommonLookup.LOSS_OF_PAY_TYPE)) {

					addingLeaves = Float.valueOf(selectedEmployeeLeave.getNoOfDays());
					toBeAddedLeaves = leaveBalance.getBalance()-addingLeaves;

				} else {
					addingLeaves = Float.valueOf(selectedEmployeeLeave.getNoOfDays());
					toBeAddedLeaves = leaveBalance.getBalance() + addingLeaves;
				}

				selectedEmployeeLeave.setApprovalStatus(CommonLookup.LEAVE_PM_CANCELLED_STATUS);
				leaveBalance.setBalance(toBeAddedLeaves);
				leaveBalance.getLeaveType().setLeaveTypeid(selectedEmployeeLeave.getLeaveType().getLeaveTypeid());
				leaveBalanceDAO.updateLeaveBalance(leaveBalance);
			} else {
				System.out.println("REJECTED CLICKED-------------------------------->");
				selectedEmployeeLeave.setApprovalStatus(CommonLookup.LEAVE_REJECT_STATUS);
			}
			if(actionEvent.getComponent().getId().equals("Accept")){
			   employeeLeaveDAO.leaveApprovedByApprovalComments(selectedEmployeeLeave);
			   updateTimesheetDetailStatusIfExist();
			}else{
				employeeLeaveDAO.leaveApprovedByApprovalComments(selectedEmployeeLeave);
			}
            
			Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
			emailUtil = new EmailUtil();
			if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
				System.out.println("PRODUCTION");
				String[] to = { employeeLeave.getEmployee().getEmail() };
				emailUtil.setRecieverAddresses(to);				
			} else {
				System.out.println("DEVELOPMENT");
				String[] to = { hostsMap.get(CommonLookup.DEV_EMAIL) };
				emailUtil.setRecieverAddresses(to);				
			}

			String subject = null;
			if (actionEvent.getComponent().getId().equals("Accept")) {
				subject = selectedEmployeeLeave.getEmployee().getFirstName()
						+ " (" + selectedEmployeeLeave.getLeaveCode() + ") "
						+ " Leave Cancellation Request is Approved ";
				messageString = "Dear "
						+ selectedEmployeeLeave.getEmployee().getFirstName()
						+ " "
						+ selectedEmployeeLeave.getEmployee().getLastName()
						+ ",<br /><br />" +

						"<table border='0'>"
						+ "<tr> <td>Leave Description </td><td> : </td><td>"
						+ selectedEmployeeLeave.getReasonForCancel()
						+ " </td></tr>"
						+ "<tr> <td>Leave Type</td><td> : </td> <td>"
						+ selectedEmployeeLeave.getLeaveType().getLeaveType()
						+ "</td></tr>" +

						"</table><br/><br/>" +

						"Your leave cancellation request is approved from "
						+ leaveFROM + " to " + leaveTO
						+ " with approver comments : "
						+ selectedEmployeeLeave.getCancellationComments()
						+ "<br /><br />" +

						"Thanks & Regards, <br />"
						+ CommonLookup.WAM_DEVTEAM_SIGNATURE;
			} else {
				System.out.println("COMPONENT ID IS :"
						+ actionEvent.getComponent().getId());
				subject = selectedEmployeeLeave.getEmployee().getFirstName()
						+ " (" + leaveCODE + ") "
						+ " Leave Cancellation Request is Rejected";
				messageString = "Dear "
						+ selectedEmployeeLeave.getEmployee().getFirstName()
						+ " "
						+ selectedEmployeeLeave.getEmployee().getLastName()
						+ ",<br /><br />" +

						"<table border='0'>"
						+ "<tr> <td>Leave Description </td><td> : </td><td>"
						+ selectedEmployeeLeave.getReasonForCancel()
						+ " </td></tr>"
						+ "<tr> <td>Leave Type</td><td> : </td> <td>"
						+ selectedEmployeeLeave.getLeaveType().getLeaveType()
						+ "</td></tr>" +

						"</table><br/><br/>" +

						"Your leave cancellation request is rejected from "
						+ leaveFROM + " to " + leaveTO
						+ " with approver comments : "
						+ selectedEmployeeLeave.getCancellationComments()
						+ "<br /><br />" +

						"Thanks & Regards, <br />"
						+ CommonLookup.WAM_DEVTEAM_SIGNATURE;
			}
			// emailUtil.setRecieverAddresses();
			emailUtil.setMessage(messageString);
			emailUtil
					.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
			emailUtil.setSubject(subject);

			if (actionEvent.getComponent().getId().equals("Accept")) {

				FacesContext
						.getCurrentInstance()
						.addMessage(
								null,
								new FacesMessage(
										FacesMessage.SEVERITY_INFO,
										" Leave cancellation request is approved  successfully.",
										""));
				RequestContext.getCurrentInstance().execute(
						"dialogToCancel.hide()");
			} else {
				FacesContext
						.getCurrentInstance()
						.addMessage(
								null,
								new FacesMessage(
										FacesMessage.SEVERITY_INFO,
										" Leave cancellation request is rejected.",
										""));
				RequestContext.getCurrentInstance().execute(
						"dialogToCancel.hide()");
			}

			SwingWorker worker = new SwingWorker<String, Object>() {

				@Override
				protected String doInBackground() throws Exception {
					// TODO Auto-generated method stub

					emailUtil.processAndSendEmail();
					return null;
				}

				@Override
				protected void done() {
					// TODO Auto-generated method stub
					System.out.println("MAIL SENT");
					// super.done();
				}
			};
			worker.execute();
			
			selectedEmployeeLeave = new EmployeeLeave();
			return "viewleaves";
		} else {
			RequestContext.getCurrentInstance()
					.execute("dialogToCancel.show()");
			return "viewleaves";
		}
		
		
	}

	public boolean validateApproval() {
		System.out.println("IN VALIDATE METHOD");
		FORM_VALID = true;
		if (ValidationUtil.isEmpty(selectedEmployeeLeave.getApprovalComments(),
				"Approver Comments", null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("LEAVE APPROVAL STATUS IS EMPTY");

			FORM_VALID = false;

		}

		return FORM_VALID;
	}

	public boolean validate() {
		System.out.println("IN VALIDATE METHOD");
		FORM_VALID = true;
		if (ValidationUtil.isEmpty(
				selectedEmployeeLeave.getCancellationComments(),
				"Cancellation Comments", null,
				FacesContext.getCurrentInstance()) != null) {
			System.out.println("LEAVE APPROVAL STATUS IS EMPTY");

			FORM_VALID = false;

		}

		return FORM_VALID;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public Map<String, Integer> getMonthsMap() {
		return monthsMap;
	}

	public void setMonthsMap(Map<String, Integer> monthsMap) {
		this.monthsMap = monthsMap;
	}

	public Map<String, Integer> getYearsMap() {
		return yearsMap;
	}

	public void setYearsMap(Map<String, Integer> yearsMap) {
		this.yearsMap = yearsMap;
	}

	public Integer getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(Integer selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public Integer getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(Integer selectedYear) {
		this.selectedYear = selectedYear;
	}

	public String[] getMonthNames() {
		return monthNames;
	}

	public void setMonthNames(String[] monthNames) {
		this.monthNames = monthNames;
	}

	public boolean isLEAVES_FOUND() {
		return LEAVES_FOUND;
	}

	public void setLEAVES_FOUND(boolean lEAVES_FOUND) {
		LEAVES_FOUND = lEAVES_FOUND;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public EmployeeLeave getSelectedEmployeeLeave() {
		return selectedEmployeeLeave;
	}

	public void setSelectedEmployeeLeave(EmployeeLeave selectedEmployeeLeave) {
		this.selectedEmployeeLeave = selectedEmployeeLeave;
	}

	public String getLeaveApprovedAlready() {
		return leaveApprovedAlready;
	}

	public void setLeaveApprovedAlready(String leaveApprovedAlready) {
		this.leaveApprovedAlready = leaveApprovedAlready;
	}

	public boolean isLeaveApprovedFlag() {
		return leaveApprovedFlag;
	}

	public void setLeaveApprovedFlag(boolean leaveApprovedFlag) {
		this.leaveApprovedFlag = leaveApprovedFlag;
	}

	public Float getActualLeaveBalance() {
		return actualLeaveBalance;
	}

	public void setActualLeaveBalance(Float actualLeaveBalance) {
		this.actualLeaveBalance = actualLeaveBalance;
	}

	public Float getCurrentLeaveBalance() {
		return currentLeaveBalance;
	}

	public void setCurrentLeaveBalance(Float currentLeaveBalance) {
		this.currentLeaveBalance = currentLeaveBalance;
	}

	public Float getDiductedLeaves() {
		return diductedLeaves;
	}

	public void setDiductedLeaves(Float diductedLeaves) {
		this.diductedLeaves = diductedLeaves;
	}

	public HtmlOutputText getLeaveApprovedAlreadyHtmlOutputText() {
		return leaveApprovedAlreadyHtmlOutputText;
	}

	public void setLeaveApprovedAlreadyHtmlOutputText(
			HtmlOutputText leaveApprovedAlreadyHtmlOutputText) {
		this.leaveApprovedAlreadyHtmlOutputText = leaveApprovedAlreadyHtmlOutputText;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public EmployeeAndReporting getEmployeeAndReporting() {
		return employeeAndReporting;
	}

	public void setEmployeeAndReporting(
			EmployeeAndReporting employeeAndReporting) {
		this.employeeAndReporting = employeeAndReporting;
	}

	public Employee getApprover() {
		return approver;
	}

	public void setApprover(Employee approver) {
		this.approver = approver;
	}

	public String getSelectedButton() {
		return selectedButton;
	}

	public void setSelectedButton(String selectedButton) {
		this.selectedButton = selectedButton;
	}

	public List<EmployeeLeave> getEmployeeLeavesToApproved() {
		return employeeLeavesToApproved;
	}

	public void setEmployeeLeavesToApproved(
			List<EmployeeLeave> employeeLeavesToApproved) {
		this.employeeLeavesToApproved = employeeLeavesToApproved;
	}

	public List<EmployeeLeave> getEmployeeLeavesToCancelled() {
		return employeeLeavesToCancelled;
	}

	public void setEmployeeLeavesToCancelled(
			List<EmployeeLeave> employeeLeavesToCancelled) {
		this.employeeLeavesToCancelled = employeeLeavesToCancelled;
	}

	public boolean isLEAVES_TO_BE_CANCELLED_FOUND() {
		return LEAVES_TO_BE_CANCELLED_FOUND;
	}

	public void setLEAVES_TO_BE_CANCELLED_FOUND(
			boolean lEAVES_TO_BE_CANCELLED_FOUND) {
		LEAVES_TO_BE_CANCELLED_FOUND = lEAVES_TO_BE_CANCELLED_FOUND;
	}

	public Float getAddingLeaves() {
		return addingLeaves;
	}

	public void setAddingLeaves(Float addingLeaves) {
		this.addingLeaves = addingLeaves;
	}

	public Float getToBeAddedLeaves() {
		return toBeAddedLeaves;
	}

	public void setToBeAddedLeaves(Float toBeAddedLeaves) {
		this.toBeAddedLeaves = toBeAddedLeaves;
	}

	public boolean isLEAVES_NOT_FOUND() {
		return LEAVES_NOT_FOUND;
	}

	public void setLEAVES_NOT_FOUND(boolean lEAVES_NOT_FOUND) {
		LEAVES_NOT_FOUND = lEAVES_NOT_FOUND;
	}

	public boolean isLEAVES_TO_BE_CANCELLED_NOT_FOUND() {
		return LEAVES_TO_BE_CANCELLED_NOT_FOUND;
	}

	public void setLEAVES_TO_BE_CANCELLED_NOT_FOUND(
			boolean lEAVES_TO_BE_CANCELLED_NOT_FOUND) {
		LEAVES_TO_BE_CANCELLED_NOT_FOUND = lEAVES_TO_BE_CANCELLED_NOT_FOUND;
	}

	public TimeZone getDefaultTimeZone() {
		return defaultTimeZone;
	}

	public void setDefaultTimeZone(TimeZone defaultTimeZone) {
		this.defaultTimeZone = defaultTimeZone;
	}

	public String getLeaveFROM() {
		return leaveFROM;
	}

	public void setLeaveFROM(String leaveFROM) {
		this.leaveFROM = leaveFROM;
	}

	public String getLeaveTO() {
		return leaveTO;
	}

	public void setLeaveTO(String leaveTO) {
		this.leaveTO = leaveTO;
	}

	public String getLeaveFromToBeDisplayed() {
		return leaveFromToBeDisplayed;
	}

	public void setLeaveFromToBeDisplayed(String leaveFromToBeDisplayed) {
		this.leaveFromToBeDisplayed = leaveFromToBeDisplayed;
	}

	public String getLeaveToToBeDisplayed() {
		return leaveToToBeDisplayed;
	}

	public void setLeaveToToBeDisplayed(String leaveToToBeDisplayed) {
		this.leaveToToBeDisplayed = leaveToToBeDisplayed;
	}

	public List<EmployeeLeave> getEmployeeLeavesToBeApproved() {
		return employeeLeavesToBeApproved;
	}

	public void setEmployeeLeavesToBeApproved(
			List<EmployeeLeave> employeeLeavesToBeApproved) {
		this.employeeLeavesToBeApproved = employeeLeavesToBeApproved;
	}

}
